import { defineStore } from 'pinia';
class tagsViewState {
  visitedViews = [];
  cachedViews = [];
}
export const tagsViewStore = defineStore('tagsView', {
  state: () => new tagsViewState(),
  actions: {
    //添加视图
    addView(view) {
      this.addVisitedView(view);
      this.addCachedView(view);
    },
    //添加已访问视图
    addVisitedView(view) {
      if (this.visitedViews.some((v) => v.path === view.path)) return;
      if (this.visitedViews.findIndex((v) => v.path === view.path) !== -1)
        return;
      this.visitedViews.push(
        Object.assign(
          {},
          { ...view },
          {
            title: view.meta.title || 'no-name',
          }
        )
      );
    },
    //添加缓存视图
    addCachedView(view) {
      if (this.cachedViews.includes(view.name)) return;
      if (!view.meta.noCache) {
        this.cachedViews.push(view.name);
      }
    },
    /*
     * 关闭当前标签页
     */
    delView(view) {
      return new Promise((resolve) => {
        this.delVisitedView(view);
        this.delCachedView(view);
        resolve({
          visitedViews: [...this.visitedViews],
        });
      });
    },
    //删除已访问视图
    delVisitedView(view) {
      return new Promise((resolve) => {
        for (const [i, v] of this.visitedViews.entries()) {
          if (v.path === view.path) {
            this.visitedViews.splice(i, 1);
            break;
          }
        }
        resolve([...this.visitedViews]);
      });
    },
    //删除缓存视图
    delCachedView(view) {
      return new Promise((resolve) => {
        const index = this.cachedViews.indexOf(view.name);
        index > -1 && this.cachedViews.splice(index, 1);
        resolve([...this.cachedViews]);
      });
    },
    /**
     * 关闭全部标签页
     */
    delAllViews() {
      return new Promise((resolve) => {
        // console.log("关闭全部标签页",111);
        this.delAllVisitedViews();
        this.delAllCachedViews();
        resolve({
          visitedViews: [...this.visitedViews],
        });
      });
    },

    //删除所有已访问视图
    delAllVisitedViews() {
      return new Promise((resolve) => {
        console.log('delAllVisitedViews');
        // 保留affix标签
        this.visitedViews = this.visitedViews.slice(0, 1);
        resolve([...this.visitedViews]);
      });
    }, // cachedViews
    //删除所有缓存视图
    delAllCachedViews() {
      return new Promise((resolve) => {
        this.cachedViews = this.cachedViews.filter(Boolean).slice(0, 1);
        resolve([...this.cachedViews]);
      });
    },
    //关闭其它标签页
    closeOtherViews(view) {
      return new Promise((resolve) => {
        this.closeOthersVisitedViews(view);
        this.closeOthersCachedViews(view);
        resolve();
      });
    },
    closeOthersVisitedViews(view) {
      return new Promise((resolve) => {
        const dashboard = this.visitedViews.slice(0, 1),
          noOthers = this.visitedViews.filter(
            (v) => v.meta.affix || v.path === view.path
          );
        this.visitedViews = [...dashboard, ...noOthers];
        resolve([...this.visitedViews]);
      });
    },
    closeOthersCachedViews(view) {
      return new Promise((resolve) => {
        const index = this.cachedViews.indexOf(view.name);
        console.log('index', index);
        const dashboard = this.cachedViews.filter(Boolean).slice(0, 1);
        if (index > -1) {
          this.cachedViews = this.cachedViews.slice(index, index + 1);
        } else {
          // if index = -1, there is no cached tags
          this.cachedViews = [];
        }
        resolve([...dashboard, ...this.cachedViews]);
      });
    },
    //更新访问视图
    updateVisitedView(view) {
      for (let v of this.visitedViews) {
        if (v.path === view.path) {
          v = Object.assign(v, view);
          break;
        }
      }
    },
    //关闭左侧或右侧
    closeLeftOrRight(views) {
      return new Promise((resolve) => {
        this.closeLeftOrRightVisitedViews(views);
        this.closeLeftOrRightCachedViews(views);
        resolve({
          visitedViews: [...this.visitedViews],
        });
      });
    },
    closeLeftOrRightVisitedViews(views) {
      return new Promise((resolve) => {
        this.visitedViews = this.visitedViews.filter(
          (element) =>
            !views.some(
              (tagViews) =>
                element.path === tagViews.path && element.name === tagViews.name
            )
        );
        resolve([...this.visitedViews]);
      });
    },
    closeLeftOrRightCachedViews(views) {
      return new Promise((resolve) => {
        this.cachedViews = this.visitedViews.filter(
          (element) =>
            !views.some(
              (tagViews) =>
                element.path === tagViews.path && element.name === tagViews.name
            )
        );
        resolve([...this.cachedViews]);
      });
    },
  },
  persist: {
    storage: sessionStorage,
    encrypt: true,
  },
});
